package com.studor.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studor.common.Result;
import com.studor.entity.Fix;
import com.studor.service.FixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/fix")
public class FixController {
    
    @Autowired
    private FixService fixService;
    
    @GetMapping("/list")
    @SaCheckLogin
    public Result<List<Fix>> list() {
        List<Fix> fixes = fixService.list();
        return Result.success(fixes);
    }
    
    @GetMapping("/page")
    @SaCheckLogin
    public Result<IPage<Fix>> page(@RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "10") Integer size) {
        Page<Fix> page = new Page<>(current, size);
        IPage<Fix> result = fixService.page(page);
        return Result.success(result);
    }
    
    @PostMapping("/add")
    @SaCheckLogin
    public Result<Void> add(@RequestBody Fix fix) {
        fix.setRepairNo(UUID.randomUUID().toString());
        fix.setSubTime(LocalDateTime.now());
        fix.setStatus("未处理");
        fixService.save(fix);
        return Result.success("提交报修成功");
    }
    
    @PutMapping("/update")
    @SaCheckLogin
    public Result<Void> update(@RequestBody Fix fix) {
        fixService.updateById(fix);
        return Result.success("更新报修信息成功");
    }
    
    @DeleteMapping("/delete/{repairNo}")
    @SaCheckLogin
    public Result<Void> delete(@PathVariable String repairNo) {
        fixService.removeById(repairNo);
        return Result.success("删除报修记录成功");
    }
}